package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzq.dao.TravelGroupMapper;
import com.zzq.dao.travelgroupTravelitemMapper;
import com.zzq.entity.PageResult;
import com.zzq.entity.QueryPageBean;
import com.zzq.pojo.*;
import com.zzq.service.TravelGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 幼儿猿大班
 * 2021/7/22
 */
@Service(interfaceClass = TravelGroupService.class)
public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupMapper travelGroupMapper;
    @Autowired
    private travelgroupTravelitemMapper tgim;

    @Override
    @Transactional
    public void save(TravelGroup travelGroup, Integer[] travelItemIds) {
        travelGroupMapper.insert(travelGroup);
        for (Integer travelItemId : travelItemIds) {
            Integer id = travelGroup.getId();
            tgim.insert(new travelgroupTravelitemKey(id,travelItemId));
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        TravelGroupExample travelGroupExample = new TravelGroupExample();
        if(queryPageBean.getQueryString()!=null && !"".equals(queryPageBean.getQueryString())){
            TravelGroupExample.Criteria criteria = travelGroupExample.createCriteria();
            criteria.andCodeEqualTo(queryPageBean.getQueryString());
            travelGroupExample.or().andHelpcodeEqualTo(queryPageBean.getQueryString());
            travelGroupExample.or().andNameLike("%"+queryPageBean.getQueryString()+"%");
        }
        List<TravelGroup> travelGroups = travelGroupMapper.selectByExample(travelGroupExample);
        PageInfo<TravelGroup> travelGroupPageInfo = new PageInfo<>(travelGroups);
        return new PageResult(travelGroupPageInfo.getTotal(),travelGroupPageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {
        travelGroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TravelGroup findById(Integer id) {
        return travelGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integer> findTravelItemTravelGroupIds(Integer id) {
        travelgroupTravelitemExample travelgroupTravelitemExample = new travelgroupTravelitemExample();
        com.zzq.pojo.travelgroupTravelitemExample.Criteria criteria = travelgroupTravelitemExample.createCriteria().andTravelgroupIdEqualTo(id);
        List<travelgroupTravelitemKey> travelgroupTravelitemKeys = tgim.selectByExample(travelgroupTravelitemExample);
        List<Integer> integers = new ArrayList<>();
        for (travelgroupTravelitemKey travelgroupTravelitemKey : travelgroupTravelitemKeys) {
            integers.add(travelgroupTravelitemKey.getTravelitemId());
        }
        return integers;
    }

    @Override
    @Transactional
    public void update(TravelGroup travelGroup, Integer[] travelItemIds) {
        travelGroupMapper.updateByPrimaryKey(travelGroup);
        travelgroupTravelitemExample tgTiExample = new travelgroupTravelitemExample();
        tgTiExample.createCriteria().andTravelgroupIdEqualTo(travelGroup.getId());
        tgim.deleteByExample(tgTiExample);
        for (Integer travelItemId : travelItemIds) {
            tgim.insertSelective(new travelgroupTravelitemKey(travelGroup.getId(),travelItemId));
        }
    }

    @Override
    public List<TravelGroup> findAll() {
       return travelGroupMapper.selectByExample(null);
    }
}
